package logic

import (
	"context"
	"go-weixin/app/guitar/model"
	"go-weixin/app/guitar/rpc/internal/svc"
	"go-weixin/app/guitar/rpc/pb/proto/guitar"

	"github.com/zeromicro/go-zero/core/logx"
	"github.com/zeromicro/x/errors"
)

type GuitarListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGuitarListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GuitarListLogic {
	return &GuitarListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GuitarListLogic) GuitarList(in *guitar.GuitarListReq) (*guitar.GuitarListResp, error) {
	limit := 10
	offset := (in.Page - 1) * 10
	guitarList := make([]model.GuitarModel, 0)
	err := l.svcCtx.ChenDb.Limit(limit, int(offset)).Find(&guitarList)
	if err != nil {
		return nil, errors.New(8000, err.Error())
	}
	var resp []*guitar.GuitarBase
	if len(guitarList) > 0 {
		for _, g := range guitarList {
			resp = append(resp, &guitar.GuitarBase{
				Id:    g.Id,
				Title: g.Title,
			})
		}
	}

	return &guitar.GuitarListResp{GuitarList: resp}, nil
}
